package com.bwie.shop.controller;

import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Shop;
import com.bwie.common.domain.ShopContract;
import com.bwie.common.domain.request.AddContractRequest;
import com.bwie.common.domain.request.AddShopRequest;
import com.bwie.common.domain.request.ShopContractRequest;
import com.bwie.common.domain.request.ShopRequest;
import com.bwie.common.domain.response.ShopContractResponse;
import com.bwie.common.domain.response.ShopResponse;
import com.bwie.common.domain.response.UserInfo;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.shop.service.ShopService;
import com.bwie.shop.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 店铺控制层
 *
 * @author <a href="mailto:2891704036@qq.com">zhiyuheng</a>
 * @version 1.0
 * @description: 店铺控制层
 * @date 2025/9/9 10:36
 */
@RestController
public class ShopController {

    @Autowired
    private ShopService service;
    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    /**
     * 店铺列表
     * @param shopRequest
     * @return
     */
    @PostMapping("shopList")
    public Result<List<ShopResponse>> shopList(@RequestBody ShopRequest shopRequest){
        return Result.success(service.shopList(shopRequest));
    }

    @GetMapping("userInfo")
    public Result<UserInfo> userInfo(){
        //获取请求头中的token
        String token = request.getHeader(TokenConstants.TOKEN);
        //获取用户标识
        String userKey = JwtUtils.getUserKey(token);
        //从redis中获取登录存储的用户信息
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return Result.success(userInfo);
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("upload")
    public Result upload(MultipartFile file){
        String shopImg = ossUtil.uploadMultipartFile(file);
        return Result.success(shopImg);
    }

    /**
     * 店铺入驻
     * @param addShopRequest
     * @return
     */
    @PostMapping("addShop")
    public Result addShop(@RequestBody @Valid AddShopRequest addShopRequest){
        service.addShop(addShopRequest);
        return Result.success();
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @GetMapping("reviewPass/{id}")
    public Result reviewPass(@PathVariable("id") Long id){
        service.reviewPass(id);
        return Result.success();
    }

    /**
     * 审核驳回
     * @param id
     * @return
     */
    @GetMapping("reviewReject/{id}")
    public Result reviewReject(@PathVariable("id") Long id){
        service.reviewReject(id);
        return Result.success();
    }

    /**
     * 店铺合同列表
     * @return
     */
    @GetMapping("shopContractList")
    public Result<List<ShopContractResponse>> shopContractList(){
        return Result.success(service.shopContractList());
    }

    /**
     * 合同续约
     * @param request
     * @return
     */
    @PostMapping("addContract")
    public Result addContract(@RequestBody @Valid AddContractRequest request){
        service.addContract(request);
        return Result.success();
    }

    /**
     * 店铺下拉框
     * @return
     */
    @GetMapping("selectShop")
    public Result<List<Shop>> selectShop(){
        return Result.success(service.selectShop());
    }

    /**
     * 合同审核通过
     * @param id
     * @return
     */
    @GetMapping("reviewContractPass/{id}")
    public Result reviewContractPass(@PathVariable("id") Long id){
        service.reviewContractPass(id);
        return Result.success();
    }

    /**
     * 合同审核驳回
     * @param id
     * @return
     */
    @GetMapping("reviewContractReject/{id}")
    public Result reviewContractReject(@PathVariable("id") Long id){
        service.reviewContractReject(id);
        return Result.success();
    }
}
